modify receive_web_request to take either 1 or 3 arguments

Albert Sun 9 年之前
父节点
当前提交
5da40533c6

+ 2 - 2
app/controllers/web_requests_controller.rb

@@ -24,7 +24,7 @@ class WebRequestsController < ApplicationController
24 24
     if user
25 25
       agent = user.agents.find_by_id(params[:agent_id])
26 26
       if agent
27
-        content, status, content_type = agent.trigger_web_request(params.except(:action, :controller, :agent_id, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers, request)
27
+        content, status, content_type = agent.trigger_web_request(request)
28 28
 
29 29
         if content.is_a?(String)
30 30
           render :text => content, :status => status || 200, :content_type => content_type || 'text/plain'
@@ -47,7 +47,7 @@ class WebRequestsController < ApplicationController
47 47
       secret = params[:secret]
48 48
       user.agents.of_type(Agents::UserLocationAgent).each { |agent|
49 49
         if agent.options[:secret] == secret
50
-          agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers, request)
50
+          agent.trigger_web_request(request)
51 51
         end
52 52
       }
53 53
       render :text => "ok"

+ 10 - 5
app/models/agent.rb

@@ -95,11 +95,15 @@ class Agent < ActiveRecord::Base
95 95
     false
96 96
   end
97 97
 
98
-  def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
98
+  def receive_web_request(params, method, format)
99 99
     # Implement me in your subclass of Agent.
100 100
     ["not implemented", 404]
101 101
   end
102 102
 
103
+  # alternate method signature for receive_web_request
104
+  # def receive_web_request(request=ActionDispatch::Request.new( ... ))
105
+  # end
106
+
103 107
   # Implement me in your subclass to decide if your Agent is working.
104 108
   def working?
105 109
     raise "Implement me in your subclass"
@@ -149,7 +153,8 @@ class Agent < ActiveRecord::Base
149 153
     end
150 154
   end
151 155
 
152
-  def trigger_web_request(params, method, format, headers, request)
156
+  def trigger_web_request(request)
157
+    params = request.params.except(:action, :controller, :agent_id, :user_id, :format)
153 158
     if respond_to?(:receive_webhook)
154 159
       Rails.logger.warn "DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request."
155 160
       receive_webhook(params).tap do
@@ -157,10 +162,10 @@ class Agent < ActiveRecord::Base
157 162
         save!
158 163
       end
159 164
     else
160
-      if method(:receive_web_request).arity == 3
161
-        handled_request = receive_web_request(params, method, format)
165
+      if method(:receive_web_request).arity == 1
166
+        handled_request = receive_web_request(request)
162 167
       else
163
-        handled_request = receive_web_request(params, method, format, headers, request)
168
+        handled_request = receive_web_request(params, request.method_symbol.to_s, request.format)
164 169
       end
165 170
       handled_request.tap do
166 171
         self.last_web_request_at = Time.now

+ 1 - 1
app/models/agents/twilio_agent.rb

@@ -81,7 +81,7 @@ module Agents
81 81
       "#{server_url}/users/#{user.id}/web_requests/#{id}/#{secret}"
82 82
     end
83 83
 
84
-    def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
84
+    def receive_web_request(params, method, format)
85 85
       if memory['pending_calls'].has_key? params['secret']
86 86
         response = Twilio::TwiML::Response.new {|r| r.Say memory['pending_calls'][params['secret']], :voice => 'woman'}
87 87
         memory['pending_calls'].delete params['secret']

+ 1 - 1
app/models/agents/webhook_agent.rb

@@ -45,7 +45,7 @@ module Agents
45 45
       }
46 46
     end
47 47
 
48
-    def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
48
+    def receive_web_request(params, method, format)
49 49
       # check the secret
50 50
       secret = params.delete('secret')
51 51
       return ["Not Authorized", 401] unless secret == interpolated['secret']

+ 1 - 1
spec/controllers/web_requests_controller_spec.rb

@@ -5,7 +5,7 @@ describe WebRequestsController do
5 5
     cannot_receive_events!
6 6
     cannot_be_scheduled!
7 7
 
8
-    def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
8
+    def receive_web_request(params, method, format)
9 9
       if params.delete(:secret) == options[:secret]
10 10
         memory[:web_request_values] = params
11 11
         memory[:web_request_format] = format

+ 30 - 11
spec/models/agent_spec.rb

@@ -729,34 +729,47 @@ describe Agent do
729 729
         @agent.user = users(:bob)
730 730
         @agent.save!
731 731
 
732
-        def @agent.receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
733
-          memory['last_request'] = [params, method, format, headers]
732
+        def @agent.receive_web_request(params, method, format)
733
+          memory['last_request'] = [params, method, format]
734 734
           ['Ok!', 200]
735 735
         end
736 736
       end
737 737
 
738 738
       it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do
739
-        @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({}))
740
-        expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html", {} ])
739
+        request = ActionDispatch::Request.new({
740
+          'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
741
+          'REQUEST_METHOD' => "POST",
742
+          'HTTP_ACCEPT' => 'text/html'
743
+        })
744
+
745
+        @agent.trigger_web_request(request)
746
+        expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html" ])
741 747
         expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
742 748
       end
743 749
     end
744 750
 
745
-    context "when .receive_web_request is defined without headers or request" do
751
+    context "when .receive_web_request is defined with just request" do
746 752
       before do
747 753
         @agent = Agents::WebRequestReceiver.new(:name => "something")
748 754
         @agent.user = users(:bob)
749 755
         @agent.save!
750 756
 
751
-        def @agent.receive_web_request(params, method, format)
752
-          memory['last_request'] = [params, method, format]
757
+        def @agent.receive_web_request(request)
758
+          memory['last_request'] = [request.params, request.method_symbol.to_s, request.format, {'HTTP_X_CUSTOM_HEADER' => request.headers['HTTP_X_CUSTOM_HEADER']}]
753 759
           ['Ok!', 200]
754 760
         end
755 761
       end
756 762
 
757
-      it "calls the .trigger_web_request with headers, but they don't get passed to .receive_web_request" do
758
-        @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({}))
759
-        expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html" ])
763
+      it "calls the .trigger_web_request with headers, and they get passed to .receive_web_request" do
764
+        request = ActionDispatch::Request.new({
765
+          'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
766
+          'REQUEST_METHOD' => "POST",
767
+          'HTTP_ACCEPT' => 'text/html',
768
+          'HTTP_X_CUSTOM_HEADER' => "foo"
769
+        })
770
+
771
+        @agent.trigger_web_request(request)
772
+        expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html", {'HTTP_X_CUSTOM_HEADER' => "foo"} ])
760 773
         expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
761 774
       end
762 775
     end
@@ -774,8 +787,14 @@ describe Agent do
774 787
       end
775 788
 
776 789
       it "outputs a deprecation warning and calls .receive_webhook with the params" do
790
+        request = ActionDispatch::Request.new({
791
+          'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
792
+          'REQUEST_METHOD' => "POST",
793
+          'HTTP_ACCEPT' => 'text/html'
794
+        })
795
+
777 796
         mock(Rails.logger).warn("DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request.")
778
-        @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({}))
797
+        @agent.trigger_web_request(request)
779 798
         expect(@agent.reload.memory['last_webhook_request']).to eq({ "some_param" => "some_value" })
780 799
         expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
781 800
       end